<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ASP.NET Core Logging with Application Insights</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        .code-segments, .answer-area {
            border: 1px solid #ddd;
            padding: 10px;
            min-width: 200px;
            min-height: 300px;
        }
        .code-segment {
            background-color: #f0f0f0;
            padding: 8px;
            margin: 5px;
            cursor: move;
            font-family: monospace;
        }
        .code-segment.dragging {
            opacity: 0.5;
        }
        button {
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        #answer {
            display: none;
            margin-top: 20px;
            padding: 10px;
            background-color: #f8f9fa;
            border-left: 4px solid #007bff;
        }
        table {
            border-collapse: collapse;
            width: 100%;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .drop-zone {
            border: 1px dashed #aaa;
            padding: 2px;
            display: inline-block;
            min-width: 100px;
            background-color: #fff;
            vertical-align: middle;
        }
        .code-block {
            font-family: monospace;
            white-space: pre;
            background-color: #f5f5f5;
            padding: 10px;
            border-radius: 4px;
        }
        .blank-drop {
            display: inline-block;
            width: 100px;
            height: 20px;
            border: 1px dashed #aaa;
            background-color: #f9f9f9;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <h3>QUESTION NO: 128 DRAG DROP</h3>
    <p>You are developing an ASP.NET Core Web API web service that uses Azure Application Insights to monitor performance and track events.</p>
    <p>You need to enable logging and ensure that log messages can be correlated to events tracked by Application Insights.</p>
    <p>How should you complete the code? To answer, drag the appropriate code segments to the correct locations. Each code segment may be used once, more than once, or not at all.</p>
    <p><em>NOTE: Each correct selection is worth one point.</em></p>

    <div class="container">
        <div class="code-segments" id="code-segments">
            <h4>Code segments</h4>
            <div class="code-segment" draggable="true" data-code="IncludeEventID">IncludeEventID</div>
            <div class="code-segment" draggable="true" data-code="ServerFeatures">ServerFeatures</div>
            <div class="code-segment" draggable="true" data-code="LoggerFilterOptions">LoggerFilterOptions</div>
            <div class="code-segment" draggable="true" data-code="ApplicationServices">ApplicationServices</div>
            <div class="code-segment" draggable="true" data-code="ApplicationInsightsLoggerOptions">ApplicationInsightsLoggerOptions</div>
            <div class="code-segment" draggable="true" data-code="TraceExceptionAsExceptionTelemetry">TraceExceptionAsExceptionTelemetry</div>
        </div>
        
        <div class="answer-area" id="answer-area">
            <h4>Answer Area</h4>
            <div class="code-block">
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOptions&lt;<span class="blank-drop" data-target="box1"></span>&gt;();
        Configure(o => o.<span class="blank-drop" data-target="box2"></span> = true);
        services.AddMvc();
    }
    
    public void Configure(IApplicationBuilder app,
        IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddApplicationInsights(app.<span class="blank-drop" data-target="box3"></span>,
            LogLevel.Trace);
        app.UseMvc();
    }
}
            </div>
        </div>
    </div>

    <button onclick="showAnswer()">查看答案</button>
    
    <div id="answer">
        <p><strong>正确答案：</strong></p>
        <table>
            <tr>
                <th>位置</th>
                <th>代码段</th>
            </tr>
            <tr>
                <td>Box1</td>
                <td>ApplicationInsightsLoggerOptions</td>
            </tr>
            <tr>
                <td>Box2</td>
                <td>IncludeEventID</td>
            </tr>
            <tr>
                <td>Box3</td>
                <td>ApplicationServices</td>
            </tr>
        </table>
        <p><strong>说明：</strong></p>
        <ul>
            <li><strong>Box1 (正确答案：ApplicationInsightsLoggerOptions)</strong>: ApplicationInsightsLoggerOptions 是用于配置 Application Insights 日志记录的专用选项类。
                通过 AddOptions<ApplicationInsightsLoggerOptions>() 可以注册并配置日志收集的行为（如采样率、日志级别等）。
                其他选项（如 LoggerFilterOptions）用于通用日志过滤，与 Application Insights 无直接关联。</li>
            <li><strong>Box2 (正确答案：IncludeEventID)</strong>: IncludeEventID 是 ApplicationInsightsLoggerOptions 的一个属性，设置为 true 时，日志会包含事件 ID 信息。
                此配置确保日志消息能与 Application Insights 中的事件关联（题目核心要求）。
                其他选项（如 TraceExceptionAsExceptionTelemetry）用于异常处理，与事件关联无关。</li>
            <li><strong>Box3 (正确答案：ApplicationServices)</strong>: ApplicationServices 是 IApplicationBuilder 的属性，提供对依赖注入容器的访问。
                AddApplicationInsights 需要从 DI 容器中解析 TelemetryClient 等服务，因此需传入 app.ApplicationServices。
                ServerFeatures 与服务器功能相关，不适用于日志集成场景。</li>
            <li>这些配置共同确保日志消息能与Application Insights跟踪的事件正确关联</li>
        </ul>
        <p><strong>其他选项排除理由：</strong></p>
        <ul>
            <li><strong>LoggerFilterOptions</strong>用于全局日志过滤，不特定于 Application Insights。</li>
            <li><strong>TraceExceptionAsExceptionTelemetry</strong>属于 ApplicationInsightsLoggerOptions 的子配置，但题目中需直接填写属性名。</li>
            <li><strong>ServerFeatures</strong>与服务器功能（如 Kestrel 配置）相关，与日志无关。</li>
        </ul>
        <p><strong>总结：</strong></br>以上配置实现了：</p>
        <ul>
            <li>1.注册 Application Insights 日志选项；</li>
            <li>2.启用事件 ID 关联；</li>
            <li>3.将日志工厂与 Application Insights 服务集成。</li>
            <li>确保日志消息能正确关联到 Application Insights 跟踪的事件，满足题目要求。</li>
        </ul>
    </div>

    <script>
        let draggedItem = null;

        document.querySelectorAll('.code-segment').forEach(item => {
            item.addEventListener('dragstart', function() {
                draggedItem = this;
                setTimeout(() => {
                    this.classList.add('dragging');
                }, 0);
            });

            item.addEventListener('dragend', function() {
                this.classList.remove('dragging');
            });
        });

        document.querySelectorAll('.blank-drop').forEach(zone => {
            zone.addEventListener('dragover', function(e) {
                e.preventDefault();
            });

            zone.addEventListener('dragenter', function(e) {
                e.preventDefault();
                this.style.borderColor = '#007bff';
                this.style.backgroundColor = '#e9f7fe';
            });

            zone.addEventListener('dragleave', function() {
                this.style.borderColor = '#aaa';
                this.style.backgroundColor = '#f9f9f9';
            });

            zone.addEventListener('drop', function(e) {
                e.preventDefault();
                this.style.borderColor = '#aaa';
                this.style.backgroundColor = '#f9f9f9';
                if (draggedItem) {
                    const code = draggedItem.getAttribute('data-code');
                    this.innerHTML = code;
                    this.setAttribute('data-filled', code);
                }
            });
        });

        function showAnswer() {
            document.getElementById('answer').style.display = 'block';
        }
    </script>
</body>
</html>
